<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>共{{ total }}条记录</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small">导入excel</el-button>
          <el-button type="danger" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list" :default-sort="{ prop: 'workNumber', order: 'descending' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template></el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template #default="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="prev, pager, next"
            :page-size="size"
            :total="total"
            :current-page="currentPage"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>

      <el-dialog title="新增员工" :visible.sync="showDialog" @close="hCloseDialog">
        <addor-edit ref="AddEmployee" @close="showDialog=false" @update-empolyee="hUpdateEmpolyee" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
import addorEdit from './empDialog'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 5,
      currentPage: 1,
      showDialog: false
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    formatEmployment(num) {
      // 从hireType中 通过id找到对应的元素的value
      // const item = EmployeeEnum.hireType.find(it => it.id === num)
      // if (item) {
      //   return item.value
      // } else {
      //   return '不能确定'
      // }

      const hireType = EmployeeEnum.hireType.reduce((acc, item) => {
        return { ...acc, [item.id]: item.value }
      }, {})

      console.log('枚举信息', EmployeeEnum.hireType, hireType)

      return hireType[num] || '未知'

      // return  ? '正式' : '非正式' || '未知'
    },
    async loadEmployeeList() {
      try {
        const { data: res } = await getEmployeeList(this.page, this.size)
        console.log('获取员工列表', res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    async doDel(id) {
      try {
        const { data: res } = await delEmployee(id)
        console.log('删除员工', res)
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        this.loadEmployeeList()
      } catch (err) {
        console.log(err)
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hUpdateEmpolyee() {
      this.showDialog = false
      // 添加成功，进入最后一页看数据
      this.total++
      // 最后一页
      this.page = Math.ceil(this.total / this.size)

      this.currentPage = this.page

      this.loadEmployeeList()
    },
    hCloseDialog() {
      this.$refs.AddEmployee.resetForm()
    },
    // 会自动接收当前点击的页码
    hCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployeeList()
    },
    // 每页几条
    hSizeChange(curSize) {
      // alert(size)
      // 1. 更新每页的条数
      this.size = curSize
      // 2. 重发请求
      this.loadEmployeeList()
    }
  }
}
</script>
